var accordPanels = new Array();

function AccordPanel(id, obj) {
	this._id		= null;
	this._obj		= null;
	this._state		= null;
	this._title		= null;
	this._content	= null;
	this._dropdown	= null;
	
	this.getId = function( ) { return this._id; };
	this.setId = function(t) { this._id = t;    };
	
	this.getObj = function( ) { return this._obj; };
	this.setObj = function(t) { this._obj = t;    };
	
	this.getState = function( ) { return this._state; };
	this.setState = function(t) { this._state = t;    };
	
	this.getTitle = function( ) { return this._title; };
	this.setTitle = function(t) { this._title = t;    };
	
	this.getContent = function( ) { return this._content; };
	this.setContent = function(t) { this._content = t;    };
	
	this.getDropDown = function( ) { return this._dropdown; };
	this.setDropDown = function(t) { this._dropdown = t;    };
	
	this.init = function(id, obj) {
		this.setId(id);
		this.setObj(obj);
		this.setState(ACCORD_PANEL_EXPANDED);
		// insert event
		this.insertEvents( );
	}
	
	this.insertEvents = function( ) {
		// get title
		var title = pimpas.DOM.getElementsByClassName("accordPanelTitle", "table", this.getObj());
		// onClick
		if(title.length == 0) /* error*/ return;
		this.setTitle(title[0]);
		var ref = this;
		this.getTitle().onclick = function() { ref.switchState();};
		// get content
		var content = pimpas.DOM.getElementsByClassName("accordPanelContent", "div", this.getObj());
		if(content.length == 0) /*error*/ return;
		this.setContent(content[0]);
		// get arrow
		var dropdown = pimpas.DOM.getElementsByClassName("accordPanelTitleDropDown", "span", this.getObj());
		if(dropdown.length == 0) /*error*/ return;
		var imageDropDown = pimpas.DOM.getElementsByClassName("dropdown", "img", dropdown[0]);
		if(imageDropDown.length == 0) /*error*/ return;
		this.setDropDown(imageDropDown[0]);
	}
	
	this.toString = function( ) { return "Accord Panel"; };
	
	this.init( id, obj );
}
AccordPanel.prototype.colapsePanel = function(  ) {
	// change styles
	pimpas.DOM.setClassName(this.getContent(), "accordPanelContentHidden");
	pimpas.DOM.setClassName(this.getTitle(), "accordPanelTitleContentHidden");
	
	var new_dropdown = this.getDropDown().src.replace("up_minus.png", "up_plus.png");
	this.getDropDown().src=new_dropdown;
	this.setState(ACCORD_PANEL_COLAPSED);
}
AccordPanel.prototype.initState = function(  ) {
	if(this.getState() == ACCORD_PANEL_EXPANDED)
		this.expandPanel( );
	else
		this.colapsePanel( );
}
AccordPanel.prototype.expandPanel = function(  ) {
	pimpas.DOM.setClassName(this.getContent(), "accordPanelContent");
	pimpas.DOM.setClassName(this.getTitle(), "accordPanelTitle");
	
	var new_dropdown = this.getDropDown().src.replace("up_plus.png", "up_minus.png");
	this.getDropDown().src=new_dropdown;
	this.setState(ACCORD_PANEL_EXPANDED);
}
AccordPanel.prototype.switchState = function(  ) {
	if(this.getState() == ACCORD_PANEL_EXPANDED)
		// colapse
		this.colapsePanel( );
	else
		// expand
		this.expandPanel( );
}

function initAllAccordPanels( ) {
	var clazz = pimpas.DOM.getElementsByClassName("accordPanel", "span", document);
	// init all
	var panel = null;
	for(var i=0;i<clazz.length;i++) {
		panel = new AccordPanel(clazz[i].id, clazz[i]);
		accordPanels.push(panel);
	}
}

function initAccordPanel( id, state ) {
	var obj = pimpas.DOM.getElement( id );
	if(obj != null) {
		var panel = new AccordPanel(obj.id, obj);
		panel.setState(state);
		accordPanels.push(panel);
		panel.initState( );
	}
}

var ACCORD_PANEL_EXPANDED		= 0;
var ACCORD_PANEL_COLAPSED		= 1;